package com.gccloud.design;

import com.gccloud.design.dto.ApiTestDto;
import com.gccloud.design.dto.SdApiAddDto;
import com.gccloud.design.dto.SdApiDefineDto;
import com.gccloud.design.dto.SdApiFlowRepoBindDto;
import com.gccloud.design.entity.SdApiAbilityBindEntity;
import com.gccloud.design.entity.SdApiDefineEntity;
import com.gccloud.design.entity.SdApiFlowRepoBindEntity;
import com.gccloud.design.entity.SdApiParamEntity;
import com.gccloud.design.service.SdApiDefineService;
import com.gccloud.design.vo.SdApiDefineVo;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author wu.yumin
 * @version 1.0.0
 * @date 2021/6/10 21:45
 */
@RestController
@RequestMapping("/api")
@Api(tags = "API定义")
public class SdApiDefineController {

    private static final Logger log = LoggerFactory.getLogger(SdApiDefineController.class);
    @Resource
    private SdApiDefineService sdApiDefineService;

    @PostMapping("/add")
    @ApiOperation("新增API")
    public R<Object> save(@RequestBody SdApiDefineVo vo) {
        SdApiDefineEntity entity = BeanConvertUtils.convert(vo, SdApiDefineEntity.class);
        boolean result = sdApiDefineService.insertApiDefine(entity);
        return R.success();
    }

    @PostMapping("/update")
    @ApiOperation("更新API")
    public R<Object> update(@RequestBody SdApiDefineVo vo) {
        SdApiDefineEntity entity = BeanConvertUtils.convert(vo, SdApiDefineEntity.class);
        boolean result = sdApiDefineService.updateApiDefine(entity);
        return R.success();
    }

    @PostMapping("/del/{apiId}")
    @ApiOperation("删除API")
    @ApiImplicitParam(name = "apiId", value = "接口Id", required = true, dataType = "Long", paramType = "path")
    public R<Object> delete(@PathVariable("apiId") Long apiId) {
        boolean result = sdApiDefineService.delApiDefine(apiId);
        return R.success();
    }

    /**
     * 条件分页查询 API定义
     *
     * @param sdApiDefineDto api定义dto
     * @return PageVO>
     */
    @GetMapping("/selectApiDefine")
    @ApiOperation("条件分页查询 API定义")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<Object> selectApiDefine(SdApiDefineDto sdApiDefineDto) {
        Map<String, Object> map = sdApiDefineService.selectApiDefine(sdApiDefineDto);
        PageVO<SdApiDefineVo> pageVO = (PageVO<SdApiDefineVo>) map.get("data");
        map.put("data", pageVO);
        return R.success(map);

    }

    /**
     * 删除 API定义
     *
     * @param apiId api标识
     * @return R
     */
    @PostMapping("/deleteApiDefine/{apiId}")
    @ApiOperation("删除 API定义")
    @ApiImplicitParam(name = "apiId", value = "接口Id", required = true, dataType = "Long", paramType = "path")
    public R<Boolean> deleteApiDefine(@PathVariable("apiId") Integer[] apiId) {
        boolean result = sdApiDefineService.deleteApiDefine(apiId);
        return R.success(result);
    }

    /**
     * 新增/修改 API定义
     *
     * @param dto api定义新增或修改dto
     * @return R
     */
    @PostMapping("/addOrUpApiDefine")
    @ApiOperation("新增/修改 API定义")
    public R<Boolean> addOrUpApiDefine(@RequestBody SdApiAddDto dto) {
        boolean result = sdApiDefineService.addOrUpApiDefine(dto);
        return R.success(result);
    }

    /**
     * 检查API编码是否重复
     *
     * @param apiCode api编码
     * @return R
     */
    @GetMapping("/checkApiCodeRepeat")
    @ApiOperation("检查API编码是否重复")
    public R<Boolean> checkApiCodeRepeat(String apiCode, Integer apiId) {
        boolean result = sdApiDefineService.checkApiCodeRepeat(apiCode, apiId);
        return R.success(result);
    }

    /**
     * 基于apiId查询能力绑定信息
     *
     * @param apiId api标识
     * @return SdApiAbilityBindEntity
     */
    @PostMapping("/queryAbility")
    @ApiOperation("基于apiId查询能力绑定信息")
    public R<SdApiAbilityBindEntity> queryAbility(@RequestBody Integer apiId) {
        SdApiAbilityBindEntity sdApiAbilityBindEntity = sdApiDefineService.queryAbility(apiId);
        return R.success(sdApiAbilityBindEntity);
    }

    /**
     * 参数配置
     *
     * @param entity 参数配置entity
     * @return Boolean
     */
    @PostMapping("/paramConfig")
    @ApiOperation("参数配置")
    public R<Boolean> paramConfig(@RequestBody SdApiParamEntity entity) {
        int insert = sdApiDefineService.paramConfig(entity);
        return R.success(insert > 0);
    }

    /**
     * 参数配置--模拟测试
     *
     * @param entity 参数配置entity
     * @return R
     */
    @PostMapping("/paramConfigTest")
    @ApiOperation("参数配置--模拟测试")
    public R<String> paramConfigTest(@RequestBody ApiTestDto entity) {
        String insert = sdApiDefineService.paramConfigTest(entity);
        return R.success(insert);
    }

    /**
     * 保存入参
     *
     * @param entity 参数配置entity
     * @return R
     */
    @PostMapping("/saveInparam")
    @ApiOperation("保存入参")
    public R saveInparam(@RequestBody SdApiParamEntity entity) {
        Boolean result = sdApiDefineService.saveInparam(entity);
        return R.success(result);
    }

    /**
     * 基于apiId 查询参数配置
     *
     * @param apiId api标识
     * @return SdApiParamEntity
     */
    @PostMapping("/queryParamConfig")
    @ApiOperation("基于apiId 查询参数配置")
    public R<SdApiParamEntity> queryParamConfig(@RequestBody Integer apiId) {
        SdApiParamEntity entity = sdApiDefineService.queryParamConfig(apiId);
        return R.success(entity);
    }


    /**
     * 基于apiId 查询API环节绑定信息
     *
     * @param apiId api标识
     * @return SdApiFlowRepoBindDto
     */
    @ApiOperation(value = "查询API环节绑定信息", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/apiRepo/{apiId}")
    public R<SdApiFlowRepoBindDto> queryApiRepoInfo(@PathVariable("apiId") Integer apiId) {
        SdApiFlowRepoBindDto dto = sdApiDefineService.queryApiRepoInfo(apiId);
        return R.success(dto);
    }

    /**
     * API定义生成环节
     *
     * @param dto api环节流程
     * @return R
     */
    @PostMapping("/apiRepo/bind")
    @ApiOperation("API定义生成环节")
    public R<Boolean> apiRepoBind(@RequestBody SdApiFlowRepoBindDto dto) {
        boolean result = sdApiDefineService.apiRepoBind(dto);
        return R.success(result);
    }

    /**
     * API定义生成环节修改
     *
     * @param dto api环节流程
     * @return R
     */
    @PostMapping("/apiRepo/update")
    @ApiOperation("API定义生成环节修改")
    public R<Boolean> updateApiRepoBind(@RequestBody SdApiFlowRepoBindDto dto) {
        boolean result = sdApiDefineService.updateApiRepoBind(dto);
        return R.success(result);
    }

    /**
     * API定义生成环节修改
     *
     * @param bwpId api环节流程
     * @return R
     */
    @PostMapping("/formRepo/delete/{bwpId}")
    @ApiImplicitParam(name = "bwpId", value = "环节Id", required = true, dataType = "Long", paramType = "path")
    @ApiOperation("删除环节")
    public R<Object> deleteRepoBindBwpId(@PathVariable(value = "bwpId") String bwpId) {
        sdApiDefineService.deleteRepoBindByBwpId(bwpId);
        return R.success();
    }

    /**
     * API定义 发布功能
     *
     * @param apiId api标识
     * @return R
     */
    @PostMapping("/publishOrUnline/{apiId}/{apiCode}/{optCode}")
    @ApiOperation("API发布、下线功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiId", value = "API标识", paramType = "path", required = true, dataType = "String"),
            @ApiImplicitParam(name = "apiCode", value = "API编码", paramType = "path", required = true, dataType = "String"),
            @ApiImplicitParam(name = "optCode", value = "操作编码", paramType = "path", required = true, dataType = "String")
    })
    public R<Boolean> publishApi(@PathVariable("apiId") String apiId, @PathVariable("apiCode") String apiCode,
                                 @PathVariable("optCode") String optCode) {
        boolean result = sdApiDefineService.publishApi(apiId, apiCode, optCode);
        return R.success(result);
    }

    /**
     * 基于api标识查询流程定义标识
     *
     * @param apiId api标识
     * @return List<SdApiAbilityBindEntity>
     */
    @PostMapping("/queryBwpRepoIdByApiId")
    @ApiOperation("基于api标识查询流程定义标识")
    public R<List<SdApiFlowRepoBindEntity>> queryBwpRepoIdByApiId(@RequestBody Integer[] apiId) {
        List<SdApiFlowRepoBindEntity> result = sdApiDefineService.queryBwpRepoIdByApiId(apiId);
        return R.success(result);
    }

    /**
     * 根据apiCode查询api相关信息
     */
    @GetMapping("/getApIByApiCode")
    @ApiOperation(value = "根据apiCode查询api相关信息", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<SdApiDefineVo> getApIByApiCode(@RequestParam("apiCode") String apiCode) {
        return R.success(sdApiDefineService.getApIByApiCode(apiCode));
    }

    /**
     * 根据apiCode查询api相关信息
     */
    @GetMapping("/getApIVersionByApiCode/{apiCode}")
    @ApiOperation(value = "根据apiCode查询api版本信息", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<List<SdApiDefineVo>> getApIVersionByApiCode(@PathVariable("apiCode") String apiCode) {
        return R.success(sdApiDefineService.getApIVersionByApiCode(apiCode));
    }

}
